Don't search bins in src if there's a library
authorAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 16 Jun 2017 20:38:54 +0000 (23:38 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 16 Jun 2017 20:38:54 +0000 (23:38 +0300)
Ideally we want not to search for binaries there in any case, but that's
probably not gonna happen due to backwards comparability.

src/cargo/util/toml.rs
tests/build.rs

index 66c947f1017d26f3b62146ea03c654e8bc4fd795..8450b4d4d9b4c0296bbecf5c6714fcebcdf7285f 100644 (file)
@@ -1417,7 +1417,7 @@ fn normalize(package_root: &Path,
         lib_target(&mut ret, lib);
     }
     bin_targets(&mut ret, bins,
-                &mut |bin| inferred_bin_path(bin, package_root, bins.len()));
+                &mut |bin| inferred_bin_path(bin, lib.is_some(), package_root, bins.len()));
 
 
     if let Some(custom_build) = custom_build {
@@ -1440,6 +1440,7 @@ fn normalize(package_root: &Path,
 }
 
 fn inferred_bin_path(bin: &TomlBinTarget,
+                     has_lib: bool,
                      package_root: &Path,
                      bin_len: usize) -> PathBuf {
     // here we have a single bin, so it may be located in src/main.rs, src/foo.rs,
@@ -1450,9 +1451,11 @@ fn inferred_bin_path(bin: &TomlBinTarget,
             return path.to_path_buf()
         }
 
-        let path = Path::new("src").join(&format!("{}.rs", bin.name()));
-        if package_root.join(&path).exists() {
-            return path.to_path_buf()
+        if !has_lib {
+            let path = Path::new("src").join(&format!("{}.rs", bin.name()));
+            if package_root.join(&path).exists() {
+                return path.to_path_buf()
+            }
         }
 
         let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
@@ -1469,9 +1472,11 @@ fn inferred_bin_path(bin: &TomlBinTarget,
         return path.to_path_buf()
     }
 
-    let path = Path::new("src").join(&format!("{}.rs", bin.name()));
-    if package_root.join(&path).exists() {
-        return path.to_path_buf()
+    if !has_lib {
+        let path = Path::new("src").join(&format!("{}.rs", bin.name()));
+        if package_root.join(&path).exists() {
+            return path.to_path_buf()
+        }
     }
 
     let path = Path::new("src").join("bin").join(&format!("main.rs"));
index d1326053176e74622ff1b81475898a192c457061..8b74b807f2310c03509d9cd3e4764fc38856fa79 100644 (file)
@@ -3251,3 +3251,23 @@ fn explicit_bins_without_paths() {
 
     assert_that(p.cargo_process("build"), execs().with_status(0));
 }
+
+#[test]
+fn no_bin_in_src_with_lib() {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.1.0"
+            authors = []
+
+            [[bin]]
+            name = "foo"
+        "#)
+        .file("src/lib.rs", "")
+        .file("src/foo.rs", "fn main() {}");
+
+    assert_that(p.cargo_process("build"),
+                execs().with_status(101)
+                       .with_stderr_contains(r#"[ERROR] couldn't read "src[/]bin[/]main.rs"[..]"#));
+}